﻿using System;
using RpcHelper;
using SqlExecHelper;
using Wedonek.RpcStore.Service.DAL;
using Wedonek.RpcStore.Service.Interface;
using Wedonek.RpcStore.Service.Model;

namespace Wedonek.RpcStore.Service.Collect
{
        internal class AutoTaskPlanCollect : BasicCollect<AutoTaskPlanDAL>, IAutoTaskPlanCollect
        {
                public Guid Add (AutoTaskPlan plan)
                {
                        this.CheckIsRepeat (plan.TaskId, plan.PlanOnlyNo);
                        AutoTaskDAL task = new AutoTaskDAL ();
                        using (ITransaction tran = this.GetCollect<ITransaction> ())
                        {
                                Guid id = base.BasicDAL.Add (plan);
                                task.ResetVerNum (plan.TaskId);
                                tran.Commit ();
                                return id;
                        }
                }
                public void Set (AutoTaskPlan plan, TaskPlanSet set)
                {
                        if (set.IsEquals (plan))
                        {
                                return;
                        }
                        this.CheckIsRepeat (plan.TaskId, set.PlanOnlyNo);
                        AutoTaskDAL task = new AutoTaskDAL ();
                        using (ITransaction tran = this.GetCollect<ITransaction> ())
                        {
                                base.BasicDAL.Set (plan.Id, set);
                                task.ResetVerNum (plan.TaskId);
                                tran.Commit ();
                        }
                }

                public AutoTaskPlan Get (Guid id)
                {
                        AutoTaskPlan plan = base.BasicDAL.Get (id);
                        if (plan == null)
                        {
                                throw new ErrorException ("rpc.task.plan.not.find");
                        }
                        return plan;
                }
                public AutoTaskPlan[] Gets (Guid taskId)
                {
                        return base.BasicDAL.Gets (taskId);
                }

                public void Delete (AutoTaskPlan plan)
                {
                        AutoTaskDAL task = new AutoTaskDAL ();
                        using (ITransaction tran = this.GetCollect<ITransaction> ())
                        {
                                base.BasicDAL.Delete (plan.Id);
                                task.ResetVerNum (plan.TaskId);
                                tran.Commit ();
                        }
                }

                public void CheckIsRepeat (Guid taskId, string planOnlyNo)
                {
                        if (base.BasicDAL.CheckIsRepeat (taskId, planOnlyNo))
                        {
                                throw new ErrorException ("rpc.task.plan.repeat");
                        }
                }

                public void SetIsEnable (AutoTaskPlan plan, bool isEnable)
                {
                        AutoTaskDAL task = new AutoTaskDAL ();
                        using (ITransaction tran = this.GetCollect<ITransaction> ())
                        {
                                base.BasicDAL.SetIsEnable (plan.Id,isEnable);
                                task.ResetVerNum (plan.TaskId);
                                tran.Commit ();
                        }
                }
        }
}
